package negocio;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Vector;
import util.Tools;
import base.Compromisso;
import comunicacao.ConexaoBD;
import comunicacao.InterfaceCompromissoBD;

public class ControleCompromisso {

	InterfaceCompromissoBD interfaceBD;

	public ControleCompromisso(ConexaoBD conexaoBD) {
		this.interfaceBD = new InterfaceCompromissoBD(conexaoBD);
	}

	public int cadastrarCompromisso(long idMedico, long idPaciente, String data, String horario, String descricao) throws SQLException {
		Timestamp dataT = Tools.getTimestamp(data, horario);
		if(consultarCompromissoMedico(0, idMedico, dataT) && consultarCompromissoPaciente(0, idPaciente, dataT))
			return interfaceBD.cadastrarCompromisso(idMedico, idPaciente, dataT, descricao);
		else
			return -3;
	}

	public int removerCompromisso(long id) throws SQLException {
		return interfaceBD.removerCompromisso(id);
	}

	public int removerCompromissosMedico(long idMedico) throws SQLException {
		return interfaceBD.removerCompromissosMedico(idMedico);
	}

	public int removerCompromissosPaciente(long idPaciente) throws SQLException {
		return interfaceBD.removerCompromissosPaciente(idPaciente);
	}

	public int editarCompromisso(long id, long idMedico, long idPaciente, String data, String horario,String descricao) throws SQLException {
		Timestamp dataT = Tools.getTimestamp(data, horario);
		if(consultarCompromissoMedico(id, idMedico, dataT) && consultarCompromissoPaciente(id, idPaciente, dataT))
			return interfaceBD.editarCompromisso(id, idPaciente, Tools.getTimestamp(data, horario), descricao);
		else
			return -3;
	}

	public Compromisso consultarCompromisso(long id) throws SQLException{
		return interfaceBD.consultarCompromisso(id);
	}

	public Vector<Compromisso> consultarCompromissos(long idMedico, String nomePaciente, String dataIni, String dataFim) throws SQLException {
		Timestamp dataIniSQL = null;
		Timestamp dataFimSQL = null;
		if(dataIni != null)
			dataIniSQL = Tools.getTimestamp(dataIni,true);
		if(dataFim != null)
			dataFimSQL = Tools.getTimestamp(dataFim,false);
		return interfaceBD.consultarCompromissos(idMedico, nomePaciente, dataIniSQL, dataFimSQL);
	}

	private boolean consultarCompromissoMedico(long id, long idMedico, Timestamp data) throws SQLException{
		Compromisso comp = interfaceBD.consultarCompromissoMedico(idMedico, data);
		if(comp == null || comp.getID() == id)
			return true;
		else
			return false;
	}

	private boolean consultarCompromissoPaciente(long id, long idPaciente, Timestamp data) throws SQLException {
		Compromisso comp = interfaceBD.consultarCompromissoPaciente(idPaciente, data);
		if(comp == null || comp.getID() == id)
			return true;
		else
			return false;
	}
}